package com.zhq.zhq_blog.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhq.zhq_blog.entity.Blog;
import com.zhq.zhq_blog.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 博客Mapper接口
 */
@Repository
public interface BlogMapper extends BaseMapper<Blog> {

    /**
     * 一对一关联查询
     */
    @Results(id = "blogResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "user", column = "user_id", one = @One(select = "com.zhq.zhq_blog.mapper.UserMapper.queryUserById")),
            @Result(property = "collectCount", column = "id", one = @One(select = "com.zhq.zhq_blog.mapper.CollectTblogTuserMapper.queryCollectCount")),
            @Result(property = "praiseCount", column = "id", one = @One(select = "com.zhq.zhq_blog.mapper.PraiseTblogTuserMapper.queryPraiseCount"))
    })
    @Select("SELECT * FROM tb_blog WHERE id = #{id}")
    Blog queryBlogById(@Param("id") Integer id);


    /**
     * 一对一关联查询列表
     */
    @ResultMap(value = "blogResultMap")
    @Select("SELECT * FROM tb_blog")
    List<Blog> queryAll();


    /**
     * 分页查询,并增加筛选条件
     */
    @ResultMap(value = "blogResultMap")
    @Select("select * from tb_blog ${ew.customSqlSegment}")
    IPage<Blog> queryPage(Page<Blog> page, @Param("ew") QueryWrapper<Blog> queryWrapper);

    @ResultMap(value = "blogResultMap")
    @Select("SELECT * FROM tb_blog b WHERE b.id IN ( SELECT c.blog_id FROM tb_collect_tblog_tuser c WHERE c.user_id = ${userId} ) ${ew.customSqlSegment}")
    IPage<Blog> queryCollectPage(Page<Blog> page, @Param("ew") QueryWrapper<Blog> queryWrapper,@Param("userId") int userId);

    @ResultMap(value = "blogResultMap")
    @Select("SELECT * FROM tb_blog b WHERE b.id IN ( SELECT c.blog_id FROM tb_praise_tblog_tuser c WHERE c.user_id = ${userId} ) ${ew.customSqlSegment}")
    IPage<Blog> queryPraisePage(Page<Blog> page, @Param("ew") QueryWrapper<Blog> queryWrapper,@Param("userId") int userId);
}